package com.ruoyi.web.controller.app;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.ResponseResult;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.exception.BusiException;
import com.ruoyi.common.utils.file.AliOss;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.system.domain.OrderPay;
import com.ruoyi.system.dto.AddDto;
import com.ruoyi.system.service.OrderPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Api(tags = "用户端---代付订单")
@RestController
@RequestMapping("/app/orderPay")
public class AppOrderPayController extends AppController {

    @Resource
    private OrderPayService orderPayService;

    @ApiOperation(value = "代付订单列表")
    @GetMapping("/findList")
    public ResponseResult findList() {
        this.startPage();
        OrderPay orderPay = new OrderPay();
        orderPay.setReceiveStatus(0);
        List<OrderPay> list = orderPayService.findList(orderPay);
        return this.setSuccessResult(list);
    }

    @ApiOperation(value = "已抢订单列表")
    @GetMapping("/myList")
    public ResponseResult myList() {
        PageDomain pageDomain = TableSupport.getPageDomain();

        Long userId = this.getUserId();
        OrderPay orderPay = new OrderPay();
        orderPay.setReceiveStatus(1);
        orderPay.setUserId(userId);
        pageDomain.setBeginTime(DateUtil.formatDateTime(DateUtil.beginOfDay(new Date())));
        pageDomain.setEndTime(DateUtil.now());
        List<OrderPay> list = orderPayService.findList(orderPay,pageDomain);
        return this.setSuccessResult(list);
    }

    @ApiOperation(value = "接代付订单")
    @PostMapping("/getOrder")
    public ResponseResult getOrder(@RequestBody AddDto addDto) {

        OrderPay orderPay = orderPayService.getById(addDto.getId());
        if (orderPay == null || !ObjectUtil.equal(orderPay.getReceiveStatus(), 0)) {
            throw new BusiException("已有人接单,请刷新后重试");
        }
        LoginUser loginUser = this.getLoginUser();
        orderPay.setReceiveStatus(1);
        orderPay.setUserId(loginUser.getUserId());
        orderPay.setReceiveUsername(loginUser.getUsername());
        orderPayService.updateDomain(orderPay);
        return this.setSuccessResult();
    }


    @ApiOperation(value = "上传凭证", notes = "传入订单id")
    @PostMapping("/uploadPic")
    public ResponseResult uploadPic(@RequestParam("file") MultipartFile file, @RequestParam("id") Long id) throws Exception {
        String filename = FileUploadUtils.extractFilename(file);
        String pic = AliOss.uploadToOss(file, "umashang/orderPay/" + filename);
        OrderPay orderPay = this.orderPayService.check(id);
        orderPay.setPic(pic);
        this.orderPayService.updateDomain(orderPay);
        return ResponseResult.createBySuccess();
    }


}
